package com.hzncc.flowable_diboot.rabbitmq.service.impl;

import com.hzncc.flowable_diboot.rabbitmq.service.DelayMessagePostProcessor;
import com.hzncc.flowable_diboot.rabbitmq.service.MessageQueueService;
import com.hzncc.flowable_diboot.rabbitmq.service.MqttGateway;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class RabbitMessageQueueServiceImpl implements MessageQueueService {

    private final RabbitTemplate rabbitTemplate;

    @Autowired
    private MqttGateway mqttGateway;

    @Override
    public void sendMessage(String message) {
        mqttGateway.sendToMqtt(message);
    }

    @Override
    public void sendMessage(String topic, String message) {
        mqttGateway.sendToMqtt(topic, message);
    }

    @Override
    public void sendRetainedMessage(String topic, String message) {
        mqttGateway.sendToMqtt(topic, message, true);
    }

    @Override
    public void sendDelayedMessageDirect(String exchange, String key, Object message, int timeout) {
        rabbitTemplate.convertAndSend(exchange, key, message, new DelayMessagePostProcessor(timeout));
    }

}
